﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using  PI_CS_Library.Virtual_Network_Information_Model;
using PI_CS_Tools;
using TransportLayer.Manager;

namespace  PI_CS_Library.PicsNetwork_Information_Model
{
    public class PicsLink : Link
    {
        Dictionary<int, Link> _hostedLinkList;
        Dictionary<int, double> _vnWeightList;
        PicsMatrix _srcMatrix;
        PicsMatrix _sinkMatrix;
        int _srcPort;
        int _dstPort;

        public PicsLink(PicsMatrix _srcMatrix, PicsMatrix _sinkMatrix, int _srcPort, int _dstPort, LinkParameters _parameters)
            : base(_parameters)
        {
            _id = new Identification("Physical Link: From: " + _srcMatrix + " To: " + _sinkMatrix);

            this._srcMatrix = _srcMatrix;
            this._sinkMatrix = _sinkMatrix;

            this._srcPort = _srcPort;
            this._dstPort = _dstPort;

            _hostedLinkList = new Dictionary<int, Link>();

            _vnWeightList = new Dictionary<int, double>();
        }

        public PicsLink(int _id, PicsMatrix _srcMatrix, PicsMatrix _sinkMatrix, int _srcPort, int _dstPort, LinkParameters _parameters)
            : base(_parameters)
        {
            this._id = new Identification(_id, "Physical Link: From: " + _srcMatrix + " To: " + _sinkMatrix);

            this._srcMatrix = _srcMatrix;
            this._sinkMatrix = _sinkMatrix;

            this._srcPort = _srcPort;
            this._dstPort = _dstPort;

            _hostedLinkList = new Dictionary<int, Link>();

            _vnWeightList = new Dictionary<int, double>();
        }

        public TransportLayer.Link GetTransportRepresentation()
        {
            TransportLayer.Link _newLink = new TransportLayer.Link();
            _newLink.Capacity = _parameters.Capacity;
            _newLink.Delay = _parameters.Delay;
            _newLink.ErrorRate = _parameters.ErrorRate;

            _newLink.StartNode = _srcMatrix.Id.Number;
            _newLink.EndNode = _sinkMatrix.Id.Number;

            _newLink.StartPort = _srcPort;
            _newLink.EndPort = _dstPort;

            return _newLink;
        }

        public Engine.Link GetEngineRepresentation(Engine _managerEngine)
        {
            Engine.Node _src = _managerEngine.Nodes[_srcMatrix.Id.Number];
            Engine.Node _sink = _managerEngine.Nodes[_sinkMatrix.Id.Number];

            Engine.Link _eLink = new Engine.Link(Id.Number, _src, _sink, _srcPort, _dstPort, _parameters.Capacity, _parameters.Delay, _parameters.ErrorRate);

            return _eLink;
        }

        new public PicsMatrix SrcMatrix
        {
            get { return _srcMatrix; }
            set { _srcMatrix = value; }
        }

        new public PicsMatrix SinkMatrix
        {
            get { return _sinkMatrix; }
            set { _sinkMatrix = value; }
        }

        public Dictionary<int, Link> HostedLinkList
        {
            get { return _hostedLinkList; }
            set { _hostedLinkList = value; }
        }

        public Dictionary<int, double> VnWeightList
        {
            get { return this._vnWeightList; }
            set { this._vnWeightList = value; }
        }

        public override string ToString()
        {
            return _id.ToString();
        }
    }
}
